<?php
// 大转盘
class Dazhuanpan
{

	private $prize_arr; //奖品信息
	private $proArr; //数量数组
	private $user_num; //参与人总和

	function __construct()
	{
		$con = mysqli_connect('localhost','root','root','ceshi'); //链接数据

		// 查询参与用户数
		$user_num_sqlu = "SELECT COUNT(id) FROM dazhuanpan_user";
		$user_re = mysqli_query($con,$user_num_sqlu);
		$user_num = mysqli_fetch_row($user_re)[0];
		$this->user_num = $user_num;
		
		// 查询奖品信息
		$sql = "SELECT * FROM dazhuanpan_jiangpin";
		$result = mysqli_query($con,$sql);
		$data = mysqli_fetch_all($result,MYSQLI_ASSOC);
		mysqli_free_result($result);
		$this->prize_arr = $data;
		mysqli_close($con);
		
		$proArr = '';
		// 遍历出数量数组
		foreach ($data as $k=>$v)
		{
			// 查询奖品信息 如果大于9001的时候 加入第二名奖项;
			if($k == 1)
			{
				if($user_num > 9001)
				{
					$proArr[] = $v['shuliang'];
				}else{
					$proArr[]=0;
				}
				
			}else{
				$proArr[] = $v['shuliang'];
			}
			
		}

		$this->proArr = $proArr;
	}
	/**
	 * 大转盘计算
	 * @return [type] [description]
	 */
	private function dzsuanfa()
	{
		$data = ''; 
		$proSum = array_sum($this->proArr); //概率数组的总概率精度 
		foreach ($this->proArr as $k => $v) { //概率数组循环 
	        $randNum = mt_rand(1, $proSum); 
	        if ($randNum <= $v) { 
	            $data = $id_jiangpin = $k; //中奖对应的ID
	            break; 
	        } else { 
	            $proSum -= $v;
	        } 
	    }
	    unset($proArr);
	    // 数量减一
	    $this->prize_arr[$data]['shuliang']--;
	    $shuliang = $this->prize_arr[$data]['shuliang'];
	    // 修改库存
	    $con = mysqli_connect('localhost','root','root','ceshi'); //链接数据
	    $id_jiangpin++;
	    $sql  = "UPDATE dazhuanpan_jiangpin SET shuliang = '$shuliang' WHERE id = '$id_jiangpin';";
	    // 添加中奖用户信息 (这里应该查询是否已经中奖中奖不给)
	    $sql .= "INSERT INTO dazhuanpan_user (phone,jiangpin_id) VALUE ('".$_GET["phone"]."','".$data."')";
	    $result = mysqli_multi_query($con,$sql);
	    if ($result)
	    {
	    	// mysqli_free_result($result);
			mysqli_close($con);
	    }else{
	    	$data = 'false';
	    }
	    return $data;
	}
	/**
	 * 查询用户
	 * @return [type] [description]
	 */
	private function user()
	{
		$con = mysqli_connect('localhost','root','root','ceshi'); //链接数据
		$sql  = "SELECT * FROM dazhuanpan_user";
		$result = mysqli_query($con,$sql);
		$data = mysqli_fetch_all($result,MYSQLI_ASSOC);
		mysqli_free_result($result);
		// mysqli_close($con);
		return $data;
	}
	/**
	 * 公共接口
	 * @return [type] [description]
	 */
	public function api()
	{
		// 次数达到10000 活动终止
		if ($this->user_num == 10000)
		{
			echo '活动总次数到了！';die;
		}elseif(array_sum($this->proArr) <= 0)
		{
			echo '奖品已经发放完毕！';die;
		}
		$dzsuanfa = $this->dzsuanfa();
		if($dzsuanfa == false)
		{
			echo '错误';die;
		}
		// 奖品ID
		// $jiang_id = $this->dzsuanfa();
		// echo $jiang_id;die;
		// 奖品信息
		var_dump($this->prize_arr[$dzsuanfa]);
		echo '<br>';
		echo '恭喜获得'.$this->prize_arr[$dzsuanfa]['jiangpin'];

		// 用户信息
		echo "<pre>";
		var_dump($this->user());
	}
	
}

$dz_class = new Dazhuanpan();
$dz_class->api();